//
//  CTabView.m
//  HCHK_iphone
//
//  Created by yilong zhang on 11-12-7.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "CTabView.h"

@implementation CTabBar

@synthesize delegate;
@synthesize m_FocusBtn;
@synthesize m_Btn;
@synthesize m_Panel;
@synthesize m_SelTitle;
@synthesize m_SelView;
@synthesize m_nFocus;
@synthesize m_nItemNum;

-(id)initWithFrame:(CGRect)frame Titles:(NSArray*)titles
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.backgroundColor = [UIColor clearColor];
        m_Btn = nil;
        
        UIImageView *bg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        bg.image = [UIImage imageNamed:@"tabbg.png"];
        bg.layer.shadowOffset = CGSizeMake(0, 2);
        bg.layer.shadowColor  = [[UIColor blackColor] CGColor];
        bg.layer.shadowOpacity= 0.3;
        [self addSubview:bg];
        [bg release];
        
        m_Panel = [[UIView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        m_Panel.backgroundColor = [UIColor clearColor];
        [self addSubview:m_Panel];
        [m_Panel release];
        
        m_SelView = [[UIImageView alloc] initWithFrame:CGRectMake(0, frame.size.height-8, 15, 8)];
        m_SelView.image = [UIImage imageNamed:@"tabfocus.png"];
        [self addSubview:m_SelView];
        [m_SelView release];
    
        CGFloat  fw   = ITEM_WIDTH;
        UIButton *btn = nil;
        CGRect rect = {0, 9, ITEM_WIDTH, frame.size.height-10};
        UIFont *font = [UIFont fontWithName:@"Helvetica" size:14];
        for (int i=0; i<[titles count]; i++)
        {
            btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.backgroundColor = [UIColor clearColor];
            rect.origin.x = i*fw;
            btn.frame = rect;
            btn.titleLabel.font = font;
            [btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
            btn.tag = i;
            [btn setTitle:[titles objectAtIndex:i] forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(OnButtonDown:) forControlEvents:UIControlEventTouchUpInside];
            [m_Panel addSubview:btn];
        }
        
        m_nItemNum = [m_Panel.subviews count];
        
        /*UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(0, frame.size.height-1, frame.size.width, 1)];
        line.image = [UIImage imageNamed:@"tabline.png"];
        line.alpha = 0.6;
        [self addSubview:line];
        [line release];*/
        
        [self SetFocus:0];
    }
    
    return self;
}

-(void)dealloc
{
    [super dealloc];
}

-(void)OnButtonDown:(UIButton*)sender
{
    [self SetFocus:sender.tag];
    
    [delegate TabBarClickIndex:sender.tag];
}

-(void)AddTabItem:(NSString*)title
{
    UIButton *btn = nil;
    
    CGRect  rect = {[m_Panel.subviews count]*ITEM_WIDTH, 9, ITEM_WIDTH, self.frame.size.height-10};
    btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.backgroundColor = [UIColor clearColor];
    btn.frame = rect;
    btn.tag = [m_Panel.subviews count];
    btn.titleLabel.font = [UIFont fontWithName:@"Helvetica" size:14];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(OnButtonDown:) forControlEvents:UIControlEventTouchUpInside];
    [m_Panel addSubview:btn];
    
    m_nItemNum = [m_Panel.subviews count];
}

-(void)DelTabItem:(int)index
{
    --m_nItemNum;
    NSArray *arr = [m_Panel subviews];
    UIButton*btn = [arr objectAtIndex:index];
    [btn removeFromSuperview];
}

-(void)DidAddTabItem
{
    if (m_Btn)
    {
        [m_Btn removeFromSuperview];
        m_Btn = nil;
    }
}

-(void)SetSelectedItem:(int)index
{
    [self SetFocus:index];
    
    [delegate TabBarClickIndex:index];
}

-(void)SetFocus:(int)index
{
    UIButton *btn = [[m_Panel subviews] objectAtIndex:m_nFocus];
    [btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];

    m_nFocus = index;
      
    btn = [[m_Panel subviews] objectAtIndex:m_nFocus];
    [btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    
    m_FocusBtn = (UIButton*)[[m_Panel subviews] objectAtIndex:index];
    m_SelView.center = CGPointMake(m_FocusBtn.center.x, m_SelView.center.y);
    
    [delegate DidTabBarFocusSeted:index];
}

@end

@implementation CTabView

@synthesize delegate;
@synthesize m_Switch;
@synthesize m_Gallery;
@synthesize m_Tab;
@synthesize m_TabPanel;
@synthesize m_bIsShow;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        // Initialization code
        UIImageView *bg = [[UIImageView alloc] initWithFrame:CGRectMake(0, TAB_HEIGHT-2, frame.size.width, frame.size.height-TAB_HEIGHT+2)];
        bg.image = [UIImage imageNamed:@"gallerybg.png"];
        [self addSubview:bg];
        [bg release];
        
        m_Gallery = [[CGalleryView alloc] initWithFrame:CGRectMake(5, TAB_HEIGHT+5, frame.size.width, GALLERY_HEIGHT) ItemSize:CGSizeMake(ICON_WIDTH, ICON_HEIGHT) Horizontal:YES];
        m_Gallery.backgroundColor = [UIColor clearColor];
        [self addSubview:m_Gallery];
        [m_Gallery release];
        
        //tabbar背景
        m_TabPanel = [[UIView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, TAB_HEIGHT)];
        [self addSubview:m_TabPanel];
        [m_TabPanel release];
        
        m_Tab = [[CTabBar alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, TAB_HEIGHT) Titles:[NSArray arrayWithObjects:@"最好", @"最近", @"最新", nil]];
        m_TabPanel.backgroundColor = [UIColor clearColor];
        m_Tab.delegate = self;
        [m_TabPanel addSubview:m_Tab];
        [m_Tab release];
        
        m_Switch = [UIButton buttonWithType:UIButtonTypeCustom];
        m_Switch.frame   = CGRectMake(frame.size.width-DRAWER_WIDTH-26, 2, DRAWER_WIDTH, DRAWER_HEIGHT);
        m_Switch.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        m_Switch.backgroundColor = [UIColor clearColor];
        [m_Switch setBackgroundImage:[UIImage imageNamed:@"tabdraweru.png"] forState:UIControlStateNormal];
        //m_Switch.transform = CGAffineTransformMakeRotation(3.14);
        [m_Switch addTarget:self action:@selector(OnButtonDown:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:m_Switch];
        
        /*UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(0, m_TabPanel.frame.size.height-2, self.frame.size.width, 2)];
        line.image = [UIImage imageNamed:@"fill.png"];
        [self addSubview:line];
        [line release];*/
        
        m_bIsShow = YES;
        [self InitTabView];
    }
    
    return self;
}

-(void)InitTabView
{
    CGRect frame = self.frame;
    
    m_TabPanel.frame = CGRectMake(0, 0, frame.size.width, TAB_HEIGHT);
    m_Tab.frame      = CGRectMake(0, 0, frame.size.width, TAB_HEIGHT);
    [m_Tab SetFocus:m_Tab.m_nFocus];
    m_Gallery.frame  = CGRectMake(5, TAB_HEIGHT+8, frame.size.width, GALLERY_HEIGHT);
    [m_Gallery ResetGallery];
}

-(void)ResetTabView
{    
    [self InitTabView];
}

-(void)OnButtonDown:(UIButton*)sender
{
    if (m_bIsShow)
    {
        [self CloseTabView];
    }
    else 
    {
        [self OpenTabView];
    }
}

-(void)TabBarClickIndex:(int)index
{
    [delegate TabViewClickIndex:index];
    [delegate DidTabFocusSeted:index];
}

-(void)DidTabBarFocusSeted:(int)index
{
    [delegate DidTabFocusSeted:index];
}

-(void)CloseTabView
{
    if (m_bIsShow)
    {
        m_Switch.enabled = NO;
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.5];
        [UIView setAnimationDelegate:self];
        [UIView setAnimationDidStopSelector:@selector(DidCloseShow)];
        //m_Switch.transform = CGAffineTransformMakeRotation(3.14);
        m_Switch.transform = CGAffineTransformMakeRotation(0);
        self.center = CGPointMake(self.center.x, self.center.y+m_Gallery.frame.size.height+8);
        [UIView commitAnimations];
    }
}

-(void)OpenTabView
{
    if (!m_bIsShow)
    {
        m_Switch.enabled = NO;
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.5];
        [UIView setAnimationDelegate:self];
        [UIView setAnimationDidStopSelector:@selector(DidCloseShow)];
        //m_Switch.transform = CGAffineTransformMakeRotation(0);
        m_Switch.transform = CGAffineTransformMakeRotation(3.14);
        self.center = CGPointMake(self.center.x, self.center.y-m_Gallery.frame.size.height-8);
        [UIView commitAnimations];
    }
}

-(void)DidCloseShow
{
    m_bIsShow = (m_bIsShow) ? (NO) : (YES);
    m_Switch.enabled = YES;
    [delegate TabViewIsOpended:m_bIsShow];
}

-(void)ShowIndicator
{
    /*if (!m_Indicator.isAnimating)
    {
        if (m_Gallery.m_nItemsNum >= 6)
        {
            [UIView beginAnimations:nil context:nil];
            m_Gallery.m_RootScroll.center = CGPointMake(m_Gallery.m_RootScroll.center.x-m_IndicatorView.frame.size.width, m_Gallery.m_RootScroll.center.y);
            m_IndicatorView.hidden = NO;
            [m_Indicator startAnimating];
            [UIView commitAnimations];
        }
        else 
        {
            m_IndicatorView.frame = CGRectMake(m_Gallery.m_nItemsNum*ICON_WIDTH, m_IndicatorView.frame.origin.y, m_IndicatorView.frame.size.width, m_IndicatorView.frame.size.height);
            m_IndicatorView.hidden = NO;
            [m_Indicator startAnimating];
        }
    }*/
    [m_Gallery ShowIndicator];
}

-(void)HiddenIndicator
{
    /*if (m_Indicator.isAnimating)
    {
        [UIView beginAnimations:nil context:nil];
        m_Gallery.m_RootScroll.center = CGPointMake(self.frame.size.width/2, m_Gallery.m_RootScroll.center.y);
        m_IndicatorView.hidden = YES;
        [m_Indicator stopAnimating];
        [UIView commitAnimations];
    }*/
    [m_Gallery HiddenIndicator];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
